package com.tudouji.framework.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.copy.domain.Copytokditem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tudouji.common.enums.CopyStatusEnums;
import com.tudouji.common.enums.IsCopyEnums;
import com.tudouji.project.copy.domain.Copyparam;
import com.tudouji.project.copy.domain.Copytokd;
import com.tudouji.project.copy.service.ICopyparamService;
import com.tudouji.project.copy.service.ICopytokdService;
import com.tudouji.project.copy.service.ICopytokditemService;
import com.tudouji.project.copy.service.impl.CopytokdServiceImpl;
import com.tudouji.project.copy.service.impl.KdPostUtils;
import com.tudouji.project.system.domain.SysConfig;
import com.tudouji.project.system.service.ISysConfigService;

import lombok.extern.slf4j.Slf4j;
/**
 * 金蝶数据同步
 * @author xiaxl
 *
 */
@Slf4j
@Component("kdSynTask")
public class KdSynTask {
	
	@Autowired
	private ICopyparamService copyparamService;
	
	@Autowired
	private ICopytokdService copytokdService;
	
	@Autowired
	private KdPostUtils kdPostUtils;
	
	@Autowired
    private ISysConfigService sysConfigService;

	@Autowired
	private ICopytokditemService copytokditemService;
	
	public void kdSynTask(){
		log.debug("___________________金蝶数据同步开始__________________");
		String proess = sysConfigService.selectConfigByKey("job_process");
		if(proess.equals("2")||proess.equals("4")||proess.equals("6")){
			SysConfig sysConfig = this.sysConfigService.selectConfigById(4L);
	        sysConfig.setConfigValue("5");
	        this.sysConfigService.updateConfig(sysConfig);
			try {
				Copyparam copyparam = new Copyparam();
		        copyparam.setIscopy(IsCopyEnums.YES.getCode());
		        List<Copyparam> copyparams = this.copyparamService.selectCopyparamList(copyparam);

				String login = kdPostUtils.login();
				if(StringUtils.isNotBlank(login)){
					for(Copyparam param:copyparams){
						log.info("--param id :{}",param.getId());
						Copytokd copy=new Copytokd();
						copy.setOrderdate(param.getStartdate());
						copy.setBusinessPrefix(param.getBusinessPrefix());
						List<Integer> stats=new ArrayList<Integer>();
						stats.add(CopyStatusEnums.WAIT.getCode());
						stats.add(CopyStatusEnums.FAIL.getCode());
						copy.setStatusList(stats);
						log.info("------------get copytokd list----------");
						List<Copytokd> copys=copytokdService.selectcopytokdAllList(copy);
						for(Copytokd c:copys){
							try {
								log.info("--copytokd code:{}",c.getCode());
								Copytokd kd=new Copytokd();
								kd.setId(c.getId());

								//查询明细数据
								Copytokditem copytokditem=new Copytokditem();
								copytokditem.setCtkdid(c.getId());
								List<Copytokditem> items=copytokditemService.selectCopytokditemList(copytokditem);

								c.setCopytokditemList(items);
								Map<String, String> result=kdPostUtils.post(c, c.getCopytokditemList());
								log.info("--result status:{}",result.get("status"));
								//成功
								if(result.get("status").equals("0")){
									kd.setStatus(CopyStatusEnums.SUCCESS.getCode());
									kd.setCopyRemark("同步成功");
								}else{
									if(result.get("msg").indexOf("业务日期不能小于当前会计期间起始日期") >=0){
										kd.setStatus(CopyStatusEnums.EXPIRED.getCode());
										kd.setCopyRemark(result.get("msg"));
									}else{
										kd.setStatus(CopyStatusEnums.FAIL.getCode());
										kd.setCopyRemark(result.get("msg"));
									}
								}
								if(c.getCopyTime()==null){
									kd.setCopyTime(new Date());
								}
								kd.setLastCopyTime(new Date());
								kd.setPushdate(DateUtils.getNowDate());
								log.info("--update kd status:{}",kd.getStatus());
								copytokdService.updateCopytokd(kd);
								log.info("--update end");
							} catch (Exception e) {
								e.printStackTrace();
							}finally{
								TimeUnit.MILLISECONDS.sleep(300L);
							}
						}
					}
				}else{
					log.error("kd login fail");
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.debug(e.getMessage());
			}
	        SysConfig sysConfig1 = this.sysConfigService.selectConfigById(4L);
            sysConfig1.setConfigValue("6");
            this.sysConfigService.updateConfig(sysConfig1);
		}else{
			log.info("__________数据准备未完成不能推送数据_____________");
		}
		log.info("___________________金蝶数据同步结束__________________");
	}
}
